package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import br.com.bean.BimestreBean;

public class BimestreDao {

public void inserir (Connection conn, BimestreBean value) throws Exception{
		
		String sql = "INSERT INTO bimestre(bimestre) VALUES (?)";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, value.getBimestre());
		ps.execute();
		ps.close();
		
	}
	
	public void alterar (Connection conn, BimestreBean value) throws Exception{
		
		String sql = "UPDATE bimestre SET bimestre=? WHERE id = ?;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, value.getBimestre());
		ps.execute();
		ps.close();

	}
	
	public void excluir (Connection conn, BimestreBean value) throws Exception {

		String sql = "DELETE FROM bimestre WHERE id = ?;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ps.executeUpdate();
	}
	
	public ArrayList<BimestreBean> getLista(Connection conn, BimestreBean value) throws Exception{

		String sql = "SELECT bimestre FROM bimestre where id = ?;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, value.getId());
		ResultSet rs = ps.executeQuery();
		ArrayList<BimestreBean> Bim = new ArrayList<BimestreBean>();
		while(rs.next()){
			BimestreBean a = new BimestreBean();
			a.setBimestre(rs.getString("bimestre"));
			Bim.add(a);
		}
		return Bim;
	}
	// select para o combo de bimestre
	public ArrayList<BimestreBean> getLista(Connection conn) throws Exception{

		String sql = "SELECT * FROM bimestre;";
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		ArrayList<BimestreBean> bimestre = new ArrayList<BimestreBean>();
		while(rs.next()){
			BimestreBean categoria = new BimestreBean();
			categoria.setId(rs.getInt("id"));
			categoria.setBimestre(rs.getString("bimestre"));
			bimestre.add(categoria);
		}
		return bimestre;
	}
	
}
